define(function() {


    var ViewModel = function() {
        var self = this;
        self.galleryUploader = new qq.FineUploader({
            debug: true,
            element: document.getElementById('fine-uploader'),
            request: { endpoint: '/FileUpload/UploadFile', method: 'POST' },
            validation: {
                allowedExtensions: ['xls', 'xlsx']
            },
            callbacks: {
                onAllComplete: function(successIDs, failIDs) {
                    console.log(successIDs);
                },
                onComplete: function(id, fileName, responseJSON) {
                    layer.alert('文件上传完成，解析中...', {
                        icon: 16,
                        shade: 0.6
                    });
                    var uploadId = responseJSON.uploadId;
                    $.cookie('uploadId', responseJSON.uploadId);
                    $.ajax({
                        url: '/order/process?uploadId=' + uploadId,
                        method: 'GET',
                        success: function(data) {
                            layer.closeAll();
                            layer.msg('解析完成', {
                                icon: 16,
                                shade: 0.6,
                                time: 1500
                            });

                            $("#div_form").show();
                            self.bind_data();
                            if (data.isError) {
                                layer.alert(data.message, { btn: ['导出无效的商品信息'] }, function() {
                                    window.open('/order/downloadInvalidOrder?uploadId='+uploadId);
                                });
                            }

                        },
                        error: function() {
                            layer.msg('解析失败，请联系管理员...', {
                                icon: 16,
                                shade: 0.6
                            });
                        }
                    })
                },
                onUpload: function() {
                    layer.alert('文件上传中', {
                        icon: 16,
                        shade: 0.6
                    });
                }
            }
        });
        var _item = { expressNumber: '', serialNumber: '', receiverName: '', harbour: '', roughWeight: '', productName: '', shipper: '', state: '', currency: '', totalPrice: '', serialNumber: '', count: '', receiverAddress: '' };
        self.edit_item = ko.observable(_item);
        self.show_edit = ko.observable(false);
        self.edit = function(_item) {
            self.edit_item(_item);
            self.show_edit(true);
            console.log(_item);
        };

        self.save = function() {
            $.post('order/update', self.edit_item(), function() {
                self.getUploadDetail();
                self.show_edit(false);
                self.edit_item(_item);
            });
            console.log('saved');
        };
        self.order_process = function(orders) {
            if (orders && orders.length && orders.length > 0) {
                $.each(orders, function(index, val) { val.hs_code = ""; });
            }
            var items = ko.mapping.fromJS(orders);
            return items();
        };
        self.DataGrid = new RIT.eW.DataGridAjax('/order/GetUploadDetail', 20, self.order_process);
        self.bind_data = function() {
            var uploadId = $.cookie('uploadId');
            var isOk = false;
            var pagesize = 30;
            var data = { uploadId: uploadId };
            self.DataGrid.GridParams.pageIndex(1);
            self.DataGrid.GetData(data);
        };

        self.send = function() {
            var data = {};
            data.uploadId = parseInt($.cookie('uploadId'));
            data.edpCompanyId = $("#ddl_edpCompany").val();
            data.logisticId = $("#ddl_logistic").val();
            data.edpPlatformId = $("#ddl_edpPlatform").val();
            data.weightUnit = $("#ddl_weight").val();
            data.jcbPort = $("#ddl_jcbports").val();
            data.applyPort = $("#ddl_applyports").val();
            data.customerId = $("#ddl_customer").val();
            data.transferRegionInSp = $('input[name="transferRegionInSp"]').filter(':checked').val();
            $.ajax({
                url: '/order/send',
                method: 'POST',
                contentType: 'application/json',
                data: JSON.stringify(data),
                success: function() {
                    layer.closeAll();
                    layer.msg('三单信息正在推送中...', {
                        icon: 16,
                        shade: 0.8
                    });
                    $.cookie('uploadId', null);
                },
                error: function() {
                    layer.alert('生成三单出现错误，请联系管理员', { icon: 2 });
                }
            });
        };

        self.init = function() {
            var uploadId = $.cookie('uploadId');
            if (uploadId) {
                self.bind_data();
            }
            console.log(uploadId);
        };

        self.init();
    }

    var model = new ViewModel();
    ko.applyBindings(model);
});